package com.generator.storageManage.storeBrowsing.controller;

import com.generator.storageManage.storeBrowsing.pojo.City;
import com.generator.storageManage.storeBrowsing.pojo.Province;
import com.generator.storageManage.storeBrowsing.pojo.User;
import com.generator.util.PageModel;
import com.generator.storageManage.storeBrowsing.pojo.OutCountObject;
import com.generator.storageManage.storeBrowsing.service.impl.StoreBrowsingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/storeBrowsing")
public class StoreBrowsingAction {

    @Autowired
    private StoreBrowsingService storeBrowsingService;

    public StoreBrowsingService getStoreBrowsingService() {
        return storeBrowsingService;
    }

    public void setStoreBrowsingService(StoreBrowsingService storeBrowsingService) {
        this.storeBrowsingService = storeBrowsingService;
    }

    /**
     * 取得所有仓库的信息
     * @param outCountObject
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("/getStorageByCondition")
    public PageModel<OutCountObject> getStorageByCondition(OutCountObject outCountObject,HttpServletRequest req){
        PageModel<OutCountObject> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutCountObject> totalOutCount = storeBrowsingService.getTotalOutCountPage(outCountObject, outCountObjectPageModel);
        if (totalOutCount.getData()!=null){
            if (!totalOutCount.getData().isEmpty()){
                return totalOutCount;
            }
        }
        return null;
    }

    //搜索功能--省份
    @ResponseBody
    @RequestMapping("/getProvince")
    public List<Province> getProvince(HttpSession session){
        Province province = new Province();
        List<Province> allProvince = storeBrowsingService.getAllProvince(province);
        return  allProvince;
    }

    //搜索功能--城市
    @ResponseBody
    @RequestMapping("/cheakprovince")
    public List<City> cheakprovince(HttpSession session, HttpServletRequest req){
        String province = req.getParameter("province");
        City city = new City();
        city.setParentname(province);
        List<City> allCity = storeBrowsingService.getAllCity(city);
        return  allCity;
    }

    //搜索功能--完整
    @ResponseBody
    @RequestMapping("/checkStatic")
    public PageModel<OutCountObject> checkStatic(HttpSession session, HttpServletRequest req){
        String checkhouse = req.getParameter("checkhouse");
        String checkaddress = req.getParameter("checkaddress");
        String checkaddresscity = req.getParameter("checkaddresscity");
        OutCountObject outCountObject = new OutCountObject();
        if(checkhouse==""&checkaddress!=""&checkaddresscity==""){
            outCountObject.setHouseaddress(checkaddress);
        }else if(checkhouse==""&checkaddress!=""&checkaddresscity!=""){
            outCountObject.setHouseaddress(checkaddress);
            outCountObject.setHouseaddresscity(checkaddresscity);
        }else if(checkhouse!=""&checkaddress==""){
            outCountObject.setHousename(checkhouse);
        }else if(checkhouse!=""&checkaddress!=""&checkaddresscity==""){
            outCountObject.setHousename(checkhouse);
            outCountObject.setHouseaddress(checkaddress);
        }else if(checkhouse!=""&checkaddress!=""&checkaddresscity!=""){
            outCountObject.setHousename(checkhouse);
            outCountObject.setHouseaddress(checkaddress);
            outCountObject.setHouseaddresscity(checkaddresscity);
        }
        PageModel<OutCountObject> outCountObjectPageModel = new PageModel<>();
        String pagenum = req.getParameter("pagenum");
        if(pagenum!=null){
            Integer valueOf = Integer.valueOf(pagenum);
            outCountObjectPageModel.setPageNum(valueOf);
        }
        PageModel<OutCountObject> totalOutCount = storeBrowsingService.getTotalOutCountPage(outCountObject, outCountObjectPageModel);
        return  totalOutCount;

    }

    //去到仓库详情页
    @ResponseBody
    @RequestMapping("/toStorageDetial")
    public String toStorageDetial(HttpServletRequest req,HttpSession session,PageModel<OutCountObject> pageModel){
        String storageId = (String)req.getParameter("storageid");
        if(storageId==null){
            return "false";
        }
        Integer id = Integer.valueOf(storageId);
        session.setAttribute("storageId",id);
        return "true";
    }

    //仓库详情页渲染数据
    @ResponseBody
    @RequestMapping("/getStorageDetial")
    public OutCountObject getStorageDetial(HttpSession session,PageModel<OutCountObject> pageModel){
        Integer storageId = (Integer)session.getAttribute("storageId");
        OutCountObject outCountObject = new OutCountObject();
        outCountObject.setId(storageId);
        PageModel<OutCountObject> totalOutCount = storeBrowsingService.getTotalOutCountPage(outCountObject, pageModel);
        return totalOutCount.getData().get(0);
    }

    //去到仓库更新详情页
    @ResponseBody
    @RequestMapping("/toStorageUpdateDetial")
    public String toStorageUpdateDetial(HttpServletRequest req,HttpSession session,PageModel<OutCountObject> pageModel){
        String storageId = (String)req.getParameter("storageid");
        if(storageId==null){
            return "false";
        }
        Integer id = Integer.valueOf(storageId);
        session.setAttribute("storageId",id);
        return "true";
    }

    //仓库更新详情页渲染数据
    @ResponseBody
    @RequestMapping("/getStorageUpdateDetial")
    public OutCountObject getStorageUpdateDetial(HttpSession session,PageModel<OutCountObject> pageModel){
        Integer storageId = (Integer)session.getAttribute("storageId");
        OutCountObject outCountObject = new OutCountObject();
        outCountObject.setId(storageId);
        PageModel<OutCountObject> totalOutCount = storeBrowsingService.getTotalOutCountPage(outCountObject, pageModel);
        return totalOutCount.getData().get(0);
    }

    //拿取用户名称（仓管职位）
    @ResponseBody
    @RequestMapping("/getUserList")
    public List<User> getUserList(){
        User user = new User(null,null, 8);
        List<User> userList = storeBrowsingService.getUserList(user);
        return userList;
    }


    //更新仓库信息
    @ResponseBody
    @RequestMapping("/StorafeUpdate")
    public Integer StorafeUpdate(HttpServletRequest req,HttpSession session){
        Integer storageId = (Integer)session.getAttribute("storageId");
        String updatestoragename = req.getParameter("updatestoragename");
        String updateaddress = req.getParameter("updateaddress");
        String udpateprovince = req.getParameter("udpateprovince");
        Integer updateprovinceid=null;
        String updatecity = req.getParameter("updatecity");
        Integer updatecityid=null;
        String updatefuzeman = req.getParameter("updatefuzeman");
        Integer updatefuzemanid=null;
        String udpatetel = req.getParameter("udpatetel");
        String updatedetail = req.getParameter("updatedetail");
        String udpatestatus = req.getParameter("udpatestatusid");
        String createman = req.getParameter("createman");
        String createtime = req.getParameter("createtime");
        Date createtime1=null;
        Province province = new Province();
        City city = new City();
        User user = new User(null, null, 8);
        List<Province> allProvince = storeBrowsingService.getAllProvince(province);
        List<City> allCity = storeBrowsingService.getAllCity(city);
        List<User> userList = storeBrowsingService.getUserList(user);
        for (City city1 : allCity) {
            if(city1.getCname().equals(updatecity)){
                updatecityid=city1.getCid();
            }
        }
        for (Province province1 : allProvince) {
            if(province1.getPname().equals(udpateprovince)){
                updateprovinceid=province1.getPid();
            }
        }
        for (User user1 : userList) {
            if(user1.getName().equals(updatefuzeman)){
                updatefuzemanid=user1.getUserid();
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            createtime1 = sdf.parse(createtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        OutCountObject outCountObject = new OutCountObject(storageId, updatestoragename, updatefuzeman, updatefuzemanid, udpatetel, udpateprovince, updateprovinceid, updatecity, updatecityid, udpatestatus, null, createtime1, createman,0, updateaddress, updatedetail);
        Integer integer = storeBrowsingService.storageUpdate(outCountObject);

        return integer;
    }

    //去到更新仓库可用状态
    @ResponseBody
    @RequestMapping("/tochangeStorageState")
    public String tochangeStorageState(HttpServletRequest req,HttpSession session,PageModel<OutCountObject> pageModel){
        String storageId = (String)req.getParameter("storageid");
        if(storageId==null){
            return "false";
        }
        Integer a = Integer.valueOf(storageId);
        session.setAttribute("storageId",a);
        return "true";
    }

    //更新仓库可用状态
    @ResponseBody
    @RequestMapping("/changeStorageState")
    public String changeStorageState(HttpServletRequest req,HttpSession session,PageModel<OutCountObject> pageModel){
        Integer storageId = (Integer)session.getAttribute("storageId");
        OutCountObject oneWareHouse = storeBrowsingService.getOneWareHouse(storageId);
        String stateid = oneWareHouse.getStateid();
        Integer valueOf = Integer.valueOf(stateid);
        if(valueOf==1){
            oneWareHouse.setStateid("2");
        }else if(valueOf==2){
            oneWareHouse.setStateid("1");
        }
        storeBrowsingService.storageUpdate(oneWareHouse);
        return "true";
    }

    //添加仓库
    @ResponseBody
    @RequestMapping("/addStorage")
    public Integer addStorage(HttpServletRequest req,HttpSession session){
        String updatestoragename = req.getParameter("updatestoragename");
        String updateaddress = req.getParameter("updateaddress");
        String udpateprovince = req.getParameter("udpateprovince");
        Integer updateprovinceid=null;
        String updatecity = req.getParameter("updatecity");
        Integer updatecityid=null;
        String updatefuzeman = req.getParameter("updatefuzeman");
        Integer updatefuzemanid=null;
        String udpatetel = req.getParameter("udpatetel");
        String updatedetail = req.getParameter("updatedetail");
        String udpatestatus = req.getParameter("udpatestatusid");
        String createman = req.getParameter("createman");
        Integer createmanid=null;
        String createtime = req.getParameter("createtime");
        Date createtime1=null;
        Province province = new Province();
        City city = new City();
        User user = new User(null, null, 8);
        User user2 = new User();
        List<Province> allProvince = storeBrowsingService.getAllProvince(province);
        List<City> allCity = storeBrowsingService.getAllCity(city);
        List<User> userList = storeBrowsingService.getUserList(user);
        List<User> userList2 = storeBrowsingService.getUserList(user2);
        for (City city1 : allCity) {
            if(city1.getCname().equals(updatecity)){
                updatecityid=city1.getCid();
            }
        }
        for (Province province1 : allProvince) {
            if(province1.getPname().equals(udpateprovince)){
                updateprovinceid=province1.getPid();
            }
        }
        for (User user1 : userList) {
            if(user1.getName().equals(updatefuzeman)){
                updatefuzemanid=user1.getUserid();
            }
        }
        for (User user1 : userList2) {
            if(user1.getName().equals(createman)){
                createmanid=user1.getUserid();
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            createtime1 = sdf.parse(createtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        OutCountObject outCountObject = new OutCountObject(0, updatestoragename, updatefuzeman, updatefuzemanid, udpatetel, udpateprovince, updateprovinceid, updatecity, updatecityid, udpatestatus, null, createtime1, createman,createmanid, updateaddress, updatedetail);
        Integer integer = storeBrowsingService.addStorage(outCountObject);

        return integer;
    }
}
